/**
 * Copyright 2012-2013 Brin.com
 * All rights reserved.
 * 
 * @project
 * @author Brin
 * @version 1.0
 * @data 2013-06-01
 */
package com.brin.core.util;

import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;
import freemarker.ext.beans.BeansWrapper;

/**
 * @description 为freemarker提供工具类
 * 
 * @author brin
 *
 */
public class ModelMethod extends HashMap<Object, Object> {

	private static final long serialVersionUID = 6027412527384040157L;

	private static ModelMethod method = new ModelMethod();
	
	private Properties model;
	
	private ModelMethod(){
		
	}
	
	public static ModelMethod getInstance(){
		return method;
	}
	
	public Properties getStaticModels(){
		return model;
	}
	
	public void setStaticModels(Properties model){
		if(this.model == null && model != null){
			this.model = model;
			Set<String> keys = this.model.stringPropertyNames();
			for(String key : keys){
				method.put(key, useStaticPackage(this.model.getProperty(key)));
			}
		}
	}
	
	public static TemplateHashModel useStaticPackage(String packageName){
		BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
		TemplateHashModel tempModel = wrapper.getStaticModels();
		TemplateHashModel fileModel = null;
		try {
			fileModel = (TemplateHashModel)tempModel.get(packageName);
		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
		return fileModel;
	}
}
